package thread;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author Duanyu
 * @date 2021-11-18 15:02
 */
public class LockDemotionTest {


    public static void main(String[] args) {
        final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        HashMap<String, String> map = new HashMap<>();

        try {
            //1.拿到写锁
            readWriteLock.writeLock().lock();
            //2.拿到读锁
            readWriteLock.readLock().lock();
            map.put("AA","AAAAA");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //3.释放写锁
            System.out.println("写锁释放");
            readWriteLock.writeLock().unlock();

            //4.释放读锁
            //System.out.println("读锁释放");
            //readWriteLock.readLock().unlock();
        }
        int i = 0;
        while (i < 5){
            new Thread(()->{
                System.out.println(map.get("AA"));
            }).start();
            i++;
        }
    }
}
